
import random
from threading import Thread
import  queue
import time
'''
线程同步，queue队列同步
'''


def producer(q:queue.Queue):
    for i in range(0,20):
        num=random.randint(1,100)
        print('生产者生产数据：{}'.format(num))
        q.put('生产者生产数据：{}'.format(num))
        time.sleep(0.1)
    q.put(1,timeout=1)
    q.task_done()

def consumer(q:queue.Queue):
    while True:
        item=q.get(timeout=1)
        print('消费者消费：{}'.format(item))
        time.sleep(0.5)
        if item==1:
            break

if __name__ == '__main__':
    q=queue.Queue(10)
    t1=Thread(target=producer,args=(q,))
    t2=Thread(target=consumer,args=(q,))
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    print('主线程执行完成！')
    pass
